<?php
	class RenrenWeb{
		const appid   = "474434";
		const key     = "55f194c48e574bf6ae812907e0ab3433";
		const secret  = "0b653f07c8ac4bee9394b49a00517d86";
		const backurl = "http://onpsummer.org/weixinInterface/redirect.php?url=http://activity.onpsummer.org/Game/renrenBind";
		public $test  = "this";

		/**
		*产生绑定人人网网址。。。
		*
		*
		*/
		public function bind(){
			$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
			if(strpos($agent,"mobile")){
				$display = "touch";
			}else{
				$display ="page";
			}
			$scope = "read_user_blog+read_user_checkin+read_user_feed+read_user_guestbook+read_user_invitation+read_user_like_history+read_user_message+read_user_notification+read_user_photo+read_user_status+read_user_album+read_user_comment+read_user_share+read_user_request+publish_blog+publish_checkin+publish_feed+publish_share+write_guestbook+send_invitation+send_request+send_message+send_notification+photo_upload+status_update+create_album+publish_comment+operate_like";
			$url = "https://graph.renren.com/oauth/authorize?client_id=".self::key."&redirect_uri=".self::backurl."&response_type=code&scope=".$scope."&display=".$display;
			return $url;
		}
		/**
		*通过code获取token及用户信息
		*
		*
		*/
		public function getToken($code){
			$url     = "https://graph.renren.com/oauth/token?grant_type=authorization_code&client_id=".self::key."&redirect_uri=".self::backurl."&client_secret=".self::secret."&code=".$code;
			//$urlsafe = "https://graph.renren.com/oauth/token?grant_type=authorization_code&client_id=".self::key."&redirect_uri=".self::backurl."&client_secret=".self::secret."&code=".$code."&state=".$标示;
			return file_get_contents($url);
		}
		/**
		*通过refresh更新token及用户信息
		*
		*
		*/
		public function refreshToken($refreshtoken){
			$url = "https://graph.renren.com/oauth/token?grant_type=refresh_token&refresh_token=".$refreshtoken."&client_id=".self::key."&client_secret=".self::secret;
			$data = file_get_contents($url);
			return $data;
		}
		//发布分享
		public function put($token,$array){
			$postfields ="ugcOwnerId=601662431&comment=".$array['content']."&ugcId=".$array['ugcid']."&ugcType=".$array['ugctype'];
			$headers = array();
			$headers [] = 'Content-type: application/x-www-form-urlencoded';
			$headers [] = "Authorization: Bearer ".$token;
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, 'https://api.renren.com/v2/share/ugc/put');
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt ($curl, CURLOPT_TIMEOUT, 20);
			curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36');
			//curl_setopt ( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
			curl_setopt ( $curl, CURLOPT_ENCODING, "" );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
			curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
			curl_setopt ( $curl, CURLOPT_HEADER, FALSE );
			curl_setopt ( $curl, CURLINFO_HEADER_OUT, TRUE );
			curl_setopt ( $curl, CURLOPT_POST, TRUE );
			curl_setopt ( $curl, CURLOPT_POSTFIELDS, $postfields );
			curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
			$result = curl_exec($curl);
			echo curl_error($curl);
			curl_close($curl);
			$result = json_decode($result,true);
			if($result["response"]["resourceUserId"]=='601662431'){
				return true;
			}else{
				return false;
			}
		}
	}